<script setup lang="ts">
import { onUnmounted, ref, onMounted } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showToast, showSuccessToast, type FormInstance, showFailToast } from 'vant'
import { loginByPassword } from '@/services/user'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
import axios from 'axios'
const store = useUserStore()
const router = useRouter()
const route = useRoute()
console.log('router', router)
console.log('route', route)
const agree = ref(false)
// 表单数据
const username = ref('16763589999')
const password = ref('qq123456')
const code = ref('')
// 控制密码是否显示
const show = ref(false)
// 登录方法(分为账号密码登录和验证码登录)
const login = async () => {
  if (!agree.value) return showToast('请勾选我已同意')
  // 校验通过,进行登录(当isPass为true时是账号密码登录)
  const res = await loginByPassword(username.value, password.value)



  if (res.resultNum === 1 as any) {
    store.setUser(res.resultObject)
    // 如果有回跳的页面就进行回跳,没有跳到个人中心
    router.push((route.query.returnUrl as string) || 'home')
    // 登录成功给个提示
    showSuccessToast(res.resultMessage)
  } else {
    showFailToast(res.resultMessage)
  }
  // 设置个人信息数据


}
// 控制显示密码还是验证码
const isPass = ref(true)

// 发送验证码
const time = ref(0)
// 定时器Id 用于后续清除定时器
let timeId: number

// 组件卸载的时候需要清空定时器,防止内存泄漏
onUnmounted(() => {
  window.clearInterval(timeId)
})

// form的ref数据
const form = ref<FormInstance>()

onMounted(() => {

})

</script>

<template>
  <div class="login-page">
    <cp-nav-bar right-text="注册" @click-right="$router.push('/register')"></cp-nav-bar>
    <div class="login-head">
      <h3>{{ '密码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">

      </a>
    </div>
    <!-- form 表单 -->
    <van-form autocomplete="off" @submit="login" ref="form">
      <van-field :rules="mobileRules" v-model="username" name="mobile" placeholder="请输入手机号" type="tel" />
      <!-- 密码 -->
      <van-field v-if="isPass" :type="show ? 'text' : 'password'" v-model="password" placeholder="请输入密码"
        :rules="passwordRules">
        <template #button>
          <cp-icon @click="show = !show" :name="`login-eye-${show ? 'on' : 'off'}`"></cp-icon>
        </template>
      </van-field>

      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button round block type="primary" native-type="submit"> 登 录 </van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }

  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;

    h3 {
      font-weight: normal;
      font-size: 24px;
    }

    a {
      font-size: 15px;
    }
  }

  &-other {
    margin-top: 60px;
    padding: 0 30px;

    .icon {
      display: flex;
      justify-content: center;

      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}

.van-form {
  padding: 0 14px;

  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;

    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }

  // van-form 下添加
  .btn-send {
    color: var(--cp-primary);

    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
